package com.jinbo.footmessage.serverInf;


import android.support.v4.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.CommentBean;
import com.jinbo.footmessage.httpbean.ProjectBean;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;

import java.util.List;

public class CommentFragment extends Fragment implements AbsListView.OnScrollListener// implements ServerInfActivity.Callbacks2{
{

	 ListView list;
	CommentItemListViewAdapter adapter;
	List<CommentBean> comments;
	private View mLoadView;


	private View footerView;
	private View loading;//尾部正在在载的布局
	private View finishLoad;
	private TextView finishText;

	/* @Override
	 public void change2(int number) {
		// TODO Auto-generated method stub;
	 }*/
	String worker_id;
	 @Override
	 public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		 worker_id=getArguments().getString("worker_id");
		View view= inflater.inflate(R.layout.fragment_comment, container, false);
		 mLoadView = view.findViewById(R.id.loading);
		 footerView = inflater.inflate(R.layout.listview_footer, null);
		 loading = footerView.findViewById(R.id.loading);//加载中，表尾巴
		 finishLoad=footerView.findViewById(R.id.load_finish);
		 finishText=(TextView)footerView.findViewById(R.id.load_all_text);

		  list=(ListView)view.findViewById(R.id.mainListView);
		 adapter=new CommentItemListViewAdapter(getActivity(),comments);
		 list.addFooterView(footerView);
		 list.setAdapter(adapter);

		 BusinessController.getInstance().getComments(worker_id,0,5, new Listener<WorkerInformationBean>() {
			 @Override
			 public void onStart(Object... params) {
				 //showProgressDialog("请稍后...");
				 mLoadView.setVisibility(View.VISIBLE);
			 }

			 @Override
			 public void onComplete(WorkerInformationBean result, Object... params) {
				 //dismissProgressDialog();
				 mLoadView.setVisibility(View.GONE);

				 finishLoad.setVisibility(View.GONE);
				 list.setOnScrollListener(CommentFragment.this);

				 comments= result.comments;
				 adapter.setcomments(comments);
				 adapter.notifyDataSetChanged();

				 if (result.comments.size() < 5) {
					 //   showToast("数据已加载完");
					 //   listView.removeFooterView(footerView);
					 loading.setVisibility(View.GONE);
					 finishLoad.setVisibility(View.VISIBLE);
					 list.setOnScrollListener(null);
					 if(result.comments.size()==0)
						 finishText.setText("暂无评论");
				 }
				 //Log.d("size",projects.size()+"");

			 }

			 @Override
			 public void onFail(final String msg, Object... params) {
				 //dismissProgressDialog();
				 mLoadView.setVisibility(View.GONE);
				 if (TextUtils.isEmpty(msg)) {
				 } else {
					 ((ServerInfActivity) getActivity()).show(msg);
				 }
			 }
		 });

		 return view;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// 当不滚动时
		if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
			// 判断是否滚动到底部
			if (view.getLastVisiblePosition() == view.getCount() - 1) {

				BusinessController.getInstance().getComments(worker_id,comments.size(),5, new Listener<WorkerInformationBean>() {
					@Override
					public void onStart(Object... params) {
						loading.setVisibility(View.VISIBLE);
						//showProgressDialog("请稍后...");
						//mLoadView.setVisibility(View.VISIBLE);
					}

					@Override
					public void onComplete(WorkerInformationBean result, Object... params) {
						//dismissProgressDialog();
						//mLoadView.setVisibility(View.GONE);

						finishLoad.setVisibility(View.GONE);
						//list.setOnScrollListener(CommentFragment.this);
						comments.addAll(result.comments);
						adapter.setcomments(comments);
						adapter.notifyDataSetChanged();

						if (result.comments.size() < 5) {
							//   showToast("数据已加载完");
							//   listView.removeFooterView(footerView);
							loading.setVisibility(View.GONE);
							finishLoad.setVisibility(View.VISIBLE);
							list.setOnScrollListener(null);

						}
						//Log.d("size",projects.size()+"");

					}

					@Override
					public void onFail(final String msg, Object... params) {
						//dismissProgressDialog();
						//mLoadView.setVisibility(View.GONE);
						loading.setVisibility(View.GONE);
						if (TextUtils.isEmpty(msg)) {
						} else {
							((ServerInfActivity) getActivity()).show(msg);
						}
					}
				});



			}
			}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

	}
/*	@Override
	public void menu2(PopupWindow menu) {
		// TODO Auto-generated method stub
		//this.menu=menu;
	}*/

}
